﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Users_MyApplyInfo : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IDCard != null)
        {
            if (!Page.IsPostBack)
            {
                bindDataTable();
            }
        }
    }
    private void bindDataTable()
    {
        DataTable dt = Exam.ApplyInfo_GetHistoryListByIdCard(IDCard);
        if (dt.Columns.Contains("ApplyStateCHN"))
            dt.Columns.Remove("ApplyStateCHN");
        dt.Columns.Add("ApplyStateCHN");
        foreach (DataRow dr in dt.Rows)
        {
            dr["ApplyStateCHN"] = int.Parse(dr["ApplyState"].ToString()) > 1 ? "已完成" : "未完成";
        }
        gv.DataSource = dt;
        gv.DataBind();
    }

    protected void operatUrl_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HyperLink hlOperateUrl = (HyperLink)e.Row.FindControl("operatUrl");
            HyperLink hlAskApply = (HyperLink)e.Row.FindControl("askApply");
            Label lbTips = (Label)e.Row.FindControl("errorTips");
            string applyState = gv.DataKeys[e.Row.RowIndex].Values[1].ToString().Trim();
            string applyId = gv.DataKeys[e.Row.RowIndex].Values[0].ToString().Trim();
            string orderId = gv.DataKeys[e.Row.RowIndex].Values[2].ToString().Trim();
            string sOrderId = security.EncryptQueryString(orderId);
            string sApplyId = security.EncryptQueryString(applyId);

            string emNum = gv.DataKeys[e.Row.RowIndex].Values[5].ToString().Trim();
            string emYear = gv.DataKeys[e.Row.RowIndex].Values[4].ToString().Trim();
            string emId = gv.DataKeys[e.Row.RowIndex].Values[3].ToString().Trim();
            string sEmNum = security.EncryptQueryString(emNum);
            string sEmYear = security.EncryptQueryString(emYear);
            string sEmId = security.EncryptQueryString(emId);

            //获取考试报名时间,超时禁止修改
            DataTable dt_time = Exam.getEmTimeByEmId(emId, emYear, emNum);
            if (dt_time.Rows.Count > 0)
            {
                DataRow dr_time = dt_time.Rows[0];
                DateTime examBeginTime = DateTime.Parse(dr_time["ApplyBeginTime"].ToString());
                DateTime examEndTime = DateTime.Parse(dr_time["ApplyEndTime"].ToString().Trim());
                if (DateTime.Compare(DateTime.Now, examBeginTime) > 0 && DateTime.Compare(DateTime.Now, examEndTime.AddDays(1)) < 0)
                {
                    if (applyState == "2")
                    {
                        hlOperateUrl.Visible = false;
                        #region 申请处理
                        DataTable dt_record = Exam.getRecordByApplyId(applyId);
                        if (dt_record.Rows.Count > 0)
                        {
                            DataRow dr_record = dt_record.Rows[0];
                            string state = dr_record["AskState"].ToString().Trim();
                            if (state == "2")
                            {
                                hlAskApply.Visible = false;
                                lbTips.Visible = true;
                                lbTips.Text = "申请已经处理完毕";
                            }
                            else if(state == "1")
                            {
                                hlAskApply.Visible = false;
                                lbTips.Visible = true;
                                lbTips.Text = "申请正在处理中";
                            }
                        }
                        else
                        {
                            hlAskApply.Visible = true;
                            hlAskApply.NavigateUrl = BSUtility.mainUrl + "Users/AskApply.aspx?ApplyID=" + sApplyId + "&EmID=" + sEmId;
                        }
                        #endregion
                    }
                    else if (applyState == "1")
                    {
                        hlAskApply.Visible = false;
                        hlOperateUrl.Visible = true;
                        hlOperateUrl.NavigateUrl = BSUtility.mainUrl + "BM/BM4.aspx?OrderID=" + sOrderId;
                    }
                    else
                    {
                        hlAskApply.Visible = false;
                        hlOperateUrl.Visible = true;
                        hlOperateUrl.NavigateUrl = BSUtility.mainUrl + "BM/BM1.aspx?EmID=" + sEmId + "&EmYear=" + sEmYear + "&EmNum=" + sEmNum;
                    }
                }
                else
                {
                    hlOperateUrl.Visible = false;
                    hlAskApply.Visible = false;
                    lbTips.Visible = true;
                    lbTips.Text = "时间已经过时!";
                }
            }
            else
            {
                hlOperateUrl.Visible = false;
                hlAskApply.Visible = false;
                lbTips.Visible = true;
                lbTips.Text = "系统出现问题!";
            }

        }
    }
}